package com.apes.hr.base.service;

import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.hr.base.model.StaffPlan;
import com.apes.hr.base.model.StaffPlanItem;
import com.apes.hr.base.model.Staffing;
import com.apes.hr.base.repository.StaffPlanRepository;
import com.apes.hr.base.repository.StaffingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 功能：编制调整计划
 *
 * @author HuJianlin
 * @create 2018-03-19 14:35
 */
@Service("hrStaffPlanService")
public class StaffPlanService extends DomainService {

    @Autowired
    public StaffPlanRepository staffPlanRepository;
    @Autowired
    public StaffingRepository staffingRepository;
    @Autowired
    public StaffingService staffingService;

    public StaffPlan save(SimpleRequest request) {
        StaffPlan staffPlan = request.getO(StaffPlan.class);

        if (staffPlan.getStaffPlanItems().size() == 0 ) throw new RuntimeException("编制调整明细数据不存在，请检查");

        staffPlan.setState("create");  //设置当前的状态
        staffPlan.setCreateUid(getPerson(request));
        staffPlan.setDateOrder(new Date());
        return staffPlanRepository.saveAndFlush(staffPlan);
    }

    /**
     * 检查数据判断，判读当前的输入版本信息
     * @param request
     */
    private void checkData(SimpleRequest request) {
        String id = request.get("id");
        int version = request.get("version");

        // 从数据库中取数，检查数据
        StaffPlan oldStaffPlan = staffPlanRepository.findOne(id);
        if (oldStaffPlan == null) throw new RuntimeException("编制计划单【" + id + "】在系统中不存在。");
        if (oldStaffPlan.getVersion() != version) throw new RuntimeException("记录已更新或已删除，请重新提交！");
    }

    public StaffPlan update(SimpleRequest request) {
        // 检查数据
        checkData(request);

        StaffPlan staffPlan = request.getO(StaffPlan.class);
        if (staffPlan.getStaffPlanItems().size() == 0 ) throw new RuntimeException("编制调整明细数据不存在，请检查");

        return staffPlanRepository.saveAndFlush(staffPlan);
    }

    /**
     * 功能：删除
     *
     * @param request
     */
    public String delete(SimpleRequest request) {
        // 检查数据
        checkData(request);
        String id = request.get("id");
        StaffPlan staffPlan = staffPlanRepository.findOne(id);
        staffPlan.setState("delete");
        staffPlan.setDeleteDate(new Date());
        staffPlanRepository.saveAndFlush(staffPlan);
        return "删除成功！";
    }

    /**
     * 功能：审核编制调整计划
     *
     * @param request
     * @return
     */
    public StaffPlan approve(SimpleRequest request) {
        // 检查数据
        StaffPlan staffPlan = request.getO(StaffPlan.class);
        checkData(request);
        String id = request.get("id");
        // 同步人员编制的数据
        updateStaffing(staffPlan);
        staffPlan.setState("done");
        staffPlan.setApproveUid(getPerson(request));
        staffPlan.setDateApprove(new Date());
        return staffPlanRepository.saveAndFlush(staffPlan);
    }


    /**
     * 同步人员编制的数据
     * @param staffPlan
     */
    public void updateStaffing(StaffPlan staffPlan) {

        for (StaffPlanItem item : staffPlan.getStaffPlanItems()) {
            Staffing staffing = new Staffing();
            List<Staffing> listStaffing = staffingRepository.findByBranchAndWorkPost(item.getBranch(), item.getWorkPost());

            // 存在 更新 不存在 创建
            if (listStaffing.size() > 0) {
                staffing = listStaffing.get(0);
            } else {
                staffing.setCompany(staffPlan.getCompany());
                staffing.setBranch(item.getBranch());
                staffing.setWorkPost(item.getWorkPost());
            }
            staffing.setDescription(item.getDescription());
            staffing.setPlanQty(item.getPlanQty());
            staffing.setStopRecruitQty(item.getStopRecruitQty());
            staffingService.create(staffing);
        }
    }
}
